#include "myspaces.h"
#include <proxydlg.h>

myNetworkAccessManager *mySpaces::s_networkAccessManager = 0;
DownloadUI *mySpaces::s_downloadManager = 0;

mySpaces::mySpaces(QWidget *parent) :
    QMainWindow(parent),
    mySpacesSettings(new MySettings()),
    WindowWidth(0)
{
    mySpacesSettings->applyProxySettings();
    setMinimumSize(320,465);
    QFile file(QLatin1String(":/myspaces/style/tabs.css"));
    file.open(QIODevice::ReadOnly);
    tabsStyle = QString(file.readAll());
    file.close();

    initForm();
}

void mySpaces::closeEvent(QCloseEvent * event)
{
    event->accept();
}

void mySpaces::initForm()
{
    tabs = new QTabWidget(this);

    tabs->setTabsClosable(true);
    connect(tabs, SIGNAL(tabCloseRequested(int)), SLOT(closeTab(int)));

    setCentralWidget(tabs);
    resize(mySpacesSettings->settings.WindowW(),
           mySpacesSettings->settings.WindowH());
    if(mySpacesSettings->settings.WindowMax())
        showMaximized();
    else
        move(mySpacesSettings->settings.WindowX(),
             mySpacesSettings->settings.WindowY());

    openNewTab(QUrl("myspaces://mainpage"));
}

CookieJar *mySpaces::cookieJar()
{
    return (CookieJar*)networkAccessManager()->cookieJar();
}

myNetworkAccessManager *mySpaces::networkAccessManager()
{
    if(!s_networkAccessManager)
    {
        s_networkAccessManager = new myNetworkAccessManager();
        s_networkAccessManager->setCookieJar(new CookieJar);
    }
    return s_networkAccessManager;
}

DownloadUI *mySpaces::downloadManager()
{
    if(!s_downloadManager)
    {
        s_downloadManager = new DownloadUI();
        s_downloadManager->setAccessManager(s_networkAccessManager);
    }
    return s_downloadManager;
}

void mySpaces::resizeEvent(QResizeEvent *event)
{
    if(event->size().width()!=WindowWidth)
    {
        WindowWidth = event->size().width();
        resizeTabs();
    }
}

QString mySpaces::GenTitle(QString intext)
{
    int c = 10;
    int textwidth = intext.length()*c;
    int tabwidth = WindowWidth/(tabs->count()+1);
    if(textwidth>tabwidth)
        intext.resize(tabwidth/c);
    QString title = intext;
    return title;
}


void mySpaces::resizeTabs()
{
    int tabwidth = WindowWidth/(tabs->count()+1);
    QString style = tabsStyle.arg(WindowWidth).arg(tabwidth);
    tabs->setStyleSheet(style);
    refreshTitles();
}

void mySpaces::openNewTab(QUrl url)
{
    for(int i = 0; i < tabs->count(); i++)
    {
        tabPage * tab = static_cast<tabPage*>(tabs->widget(i));
        if(tab->url().toString() == url.toString())
        {
            tabs->setCurrentIndex(i);
            return;
        }
    }
    tabPage * tab = new tabPage(tabs);
    tab->load(url);
    tabs->addTab(tab,QIcon(":/myspaces/t_icon_home.png"),"mySpaces");
    connect(tab, SIGNAL(OpenLinkInNewTab(QUrl)),SLOT(openNewTab(QUrl)));
    connect(tab, SIGNAL(changeTitle(QString)),
            this, SLOT(refreshTitles(QString)));
    connect(tab, SIGNAL(exitApplication()), SLOT(exitApp()));
    if(url.toString().startsWith("myspaces://"))
        tabs->setCurrentWidget(tab);
    if(tabs->count()>1)
        tabs->setMovable(true);
    else
        tabs->setMovable(false);
    resizeTabs();
}

void mySpaces::showDownloadManager()
{
    mySpaces::downloadManager()->show();
}

void mySpaces::refreshTitles(const QString)
{
    for(int i = 0; i < tabs->count(); i++)
    {
        tabPage * tab = static_cast<tabPage*>(tabs->widget(i));
        tabs->setTabText(i,GenTitle(tab->getTitle()));
        tabs->setTabToolTip(i,tab->getTitle());
    }
}

void mySpaces::refreshTitles()
{
    for(int i = 0; i < tabs->count(); i++)
    {
        tabPage * tab = static_cast<tabPage*>(tabs->widget(i));
        tabs->setTabText(i,GenTitle(tab->getTitle()));
    }
}

void mySpaces::closeTab(int index)
{
    tabPage * tab = static_cast<tabPage*>(tabs->widget(index));
    tabs->removeTab(index);
    delete tab;
    if(tabs->count()==0)
    {
        close();
        return;
    }
    if(tabs->count()==1 && tabs->count()!=0)
        tabs->setMovable(false);
    else if(tabs->count()>1 && tabs->count()!=0)
        tabs->setMovable(true);
    resizeTabs();
}

void mySpaces::exitApp()
{
    close();
}

mySpaces::~mySpaces()
{
    if(windowState()==Qt::WindowMaximized)
    {
        mySpacesSettings->settings.setWindowMax(true);
    }
    else
    {
        mySpacesSettings->settings.setWindowW(this->width());
        mySpacesSettings->settings.setWindowH(this->height());
        mySpacesSettings->settings.setWindowX(this->x());
        mySpacesSettings->settings.setWindowY(this->y());
        mySpacesSettings->settings.setWindowMax(false);
    }
    mySpacesSettings->save();
    delete s_downloadManager;
    delete s_networkAccessManager;
    delete tabs;
}
